package com.archgeek.rpc.thrfit;

import java.util.LinkedList;
import java.util.List;

/**
 * @author pizhihui
 * @date 2022-04-07 17:06
 */


class Test1 {


    public void testMethod() {

        Test1 t1 = new Test1();
        t1.my();

    }

    public static void main(String[] args) {
        Test1 t = new Test1();
        t.testMethod();
    }


    public void my() {

        String tag = getMyGrandpaStackTrace();
        System.err.println(String.format("调用我的人是:%s", tag));
    }


    public String getMyGrandpaStackTrace() {


        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        List<String> methodNames = new LinkedList<>();
        for (StackTraceElement trace : stackTrace) {
            methodNames.add(trace.getClassName() + "#" + trace.getMethodName());
        }

        System.out.println(methodNames);
        StackTraceElement father = stackTrace[1];
        StackTraceElement log = stackTrace[2];
        String tag = null;
        for (int i = 1; i < stackTrace.length; i++) {
            StackTraceElement e = stackTrace[i];
            if (!e.getClassName().equals(log.getClassName())) {
                tag = e.getClassName() + "." + e.getMethodName();
                break;
            }
        }
        if (tag == null) {
            tag = log.getClassName() + "." + log.getMethodName();

        }
        System.err.println(String.format("My father  is %s.%s", father.getClassName(), father.getMethodName()));
        System.err.println(String.format("My grandpa is %s", tag));
        return tag;

    }

}